#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <stdint.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <dlfcn.h>
#include <pthread.h>
#include <linux/fb.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <syslog.h>

// saler --> SIGUSR1 --> 开车
// saler --> SIGUSR2 --> 靠站

// driver --> SIGUSR1 --> 到总站

void handler(int sig)
{
    switch(sig)
    {
    case SIGUSR1:
        printf("开车\n");
        break;
    case SIGUSR2:
        printf("靠站\n");
        break;
    }
}

// driver
int main(int argc, char const *argv[])
{
    signal(SIGUSR1, handler);
    signal(SIGUSR2, handler);

    // 交换PID
    mkfifo("/tmp/driver2saler", 0666);
    mkfifo("/tmp/saler2driver", 0666);
    int driver2saler = open("/tmp/driver2saler", O_RDWR);
    int saler2driver = open("/tmp/saler2driver", O_RDWR);

    pid_t mypid = getpid();
    write(driver2saler, &mypid, sizeof(mypid));

    pid_t peerpid;
    read(saler2driver, &peerpid, sizeof(peerpid));

    printf("按下回车键，到总站，程序终止\n");
    getchar();

    kill(peerpid, SIGUSR1);
    return 0;
}
